Telegram Group & Telegram Channel
🧠 C# Π—Π°Π΄Π°Ρ‡Π°: β€œΠ›ΠΎΠ²ΡƒΡˆΠΊΠ° замыкания Π² циклС”

πŸ“œ УсловиС:

ΠŸΠΎΡΠΌΠΎΡ‚Ρ€ΠΈ Π½Π° этот ΠΊΠΎΠ΄ ΠΈ скаТи, Ρ‡Ρ‚ΠΎ ΠΎΠ½ Π²Ρ‹Π²Π΅Π΄Π΅Ρ‚:


using System;
using System.Collections.Generic;
using System.Linq;

class Program
{
static void Main()
{
var actions = new List<Action>();

for (int i = 0; i < 5; i++)
{
actions.Add(() => Console.Write(i + " "));
}

foreach (var action in actions)
{
action();
}
}
}


❓ Вопросы:
1. Π§Ρ‚ΠΎ Π½Π°ΠΏΠ΅Ρ‡Π°Ρ‚Π°Π΅Ρ‚ ΠΏΡ€ΠΎΠ³Ρ€Π°ΠΌΠΌΠ°?
2. ΠŸΠΎΡ‡Π΅ΠΌΡƒ Ρ€Π΅Π·ΡƒΠ»ΡŒΡ‚Π°Ρ‚ ΠΌΠΎΠΆΠ΅Ρ‚ ΡƒΠ΄ΠΈΠ²ΠΈΡ‚ΡŒ?
3. Как ΠΈΡΠΏΡ€Π°Π²ΠΈΡ‚ΡŒ ΠΊΠΎΠ΄, Ρ‡Ρ‚ΠΎΠ±Ρ‹ ΠΏΠΎΠ»ΡƒΡ‡ΠΈΡ‚ΡŒ 0 1 2 3 4?

⚠️ ΠŸΠΎΠ΄Π²ΠΎΡ…:

- ΠŸΠ΅Ρ€Π΅ΠΌΠ΅Π½Π½Π°Ρ i захватываСтся ΠΏΠΎ ссылкС всСми лямбдами Π² List<Action>, Π° Π½Π΅ копируСтся Π² ΠΌΠΎΠΌΠ΅Π½Ρ‚ добавлСния.
- Когда Ρ†ΠΈΠΊΠ» Π·Π°Π²Π΅Ρ€ΡˆΠΈΡ‚ΡΡ, i == 5, ΠΈ всС лямбды ΠΎΠ±Ρ€Π°Ρ‰Π°ΡŽΡ‚ΡΡ ΠΊ ΠΎΠ΄Π½ΠΎΠΌΡƒ ΠΈ Ρ‚ΠΎΠΌΡƒ ΠΆΠ΅ `i`, ΡƒΠΆΠ΅ Ρ€Π°Π²Π½ΠΎΠΌΡƒ 5.

---

### βœ… ΠžΠΆΠΈΠ΄Π°Π΅ΠΌΡ‹ΠΉ Π²Ρ‹Π²ΠΎΠ΄:


5 5 5 5 5


πŸ› οΈ Π˜ΡΠΏΡ€Π°Π²Π»Π΅Π½ΠΈΠ΅:

Π§Ρ‚ΠΎΠ±Ρ‹ каТдая лямбда Π·Π°Ρ…Π²Π°Ρ‚Ρ‹Π²Π°Π»Π° свою копию `i`, Π½ΡƒΠΆΠ½ΠΎ Π΄ΠΎΠ±Π°Π²ΠΈΡ‚ΡŒ ΠΏΡ€ΠΎΠΌΠ΅ΠΆΡƒΡ‚ΠΎΡ‡Π½ΡƒΡŽ ΠΏΠ΅Ρ€Π΅ΠΌΠ΅Π½Π½ΡƒΡŽ:


for (int i = 0; i < 5; i++)
{
int copy = i;
actions.Add(() => Console.Write(copy + " "));
}


Π’Π΅ΠΏΠ΅Ρ€ΡŒ Π²Ρ‹Π²ΠΎΠ΄ Π±ΡƒΠ΄Π΅Ρ‚:


0 1 2 3 4


🎯 Π§Ρ‚ΠΎ провСряСт Π·Π°Π΄Π°Ρ‡Π°:

- Π—Π½Π°Π½ΠΈΠ΅ ΠΌΠ΅Ρ…Π°Π½ΠΈΠΊΠΈ Π·Π°ΠΌΡ‹ΠΊΠ°Π½ΠΈΠΉ Π² C#
- ПониманиС Ρ€Π°Π·Π½ΠΈΡ†Ρ‹ ΠΌΠ΅ΠΆΠ΄Ρƒ значСниями ΠΈ ссылками Π² Π·Π°ΠΌΡ‹ΠΊΠ°Π½ΠΈΠΈ
- Π£ΠΌΠ΅Π½ΠΈΠ΅ ΠΎΡ‚Π»Π°ΠΆΠΈΠ²Π°Ρ‚ΡŒ Π½Π΅ΠΎΠΆΠΈΠ΄Π°Π½Π½Ρ‹Π΅ Ρ€Π΅Π·ΡƒΠ»ΡŒΡ‚Π°Ρ‚Ρ‹ Π² LINQ ΠΈ Π΄Π΅Π»Π΅Π³Π°Ρ‚Π°Ρ…



tg-me.com/csharp_1001_notes/687
Create:
Last Update:

🧠 C# Π—Π°Π΄Π°Ρ‡Π°: β€œΠ›ΠΎΠ²ΡƒΡˆΠΊΠ° замыкания Π² циклС”

πŸ“œ УсловиС:

ΠŸΠΎΡΠΌΠΎΡ‚Ρ€ΠΈ Π½Π° этот ΠΊΠΎΠ΄ ΠΈ скаТи, Ρ‡Ρ‚ΠΎ ΠΎΠ½ Π²Ρ‹Π²Π΅Π΄Π΅Ρ‚:


using System;
using System.Collections.Generic;
using System.Linq;

class Program
{
static void Main()
{
var actions = new List<Action>();

for (int i = 0; i < 5; i++)
{
actions.Add(() => Console.Write(i + " "));
}

foreach (var action in actions)
{
action();
}
}
}


❓ Вопросы:
1. Π§Ρ‚ΠΎ Π½Π°ΠΏΠ΅Ρ‡Π°Ρ‚Π°Π΅Ρ‚ ΠΏΡ€ΠΎΠ³Ρ€Π°ΠΌΠΌΠ°?
2. ΠŸΠΎΡ‡Π΅ΠΌΡƒ Ρ€Π΅Π·ΡƒΠ»ΡŒΡ‚Π°Ρ‚ ΠΌΠΎΠΆΠ΅Ρ‚ ΡƒΠ΄ΠΈΠ²ΠΈΡ‚ΡŒ?
3. Как ΠΈΡΠΏΡ€Π°Π²ΠΈΡ‚ΡŒ ΠΊΠΎΠ΄, Ρ‡Ρ‚ΠΎΠ±Ρ‹ ΠΏΠΎΠ»ΡƒΡ‡ΠΈΡ‚ΡŒ 0 1 2 3 4?

⚠️ ΠŸΠΎΠ΄Π²ΠΎΡ…:

- ΠŸΠ΅Ρ€Π΅ΠΌΠ΅Π½Π½Π°Ρ i захватываСтся ΠΏΠΎ ссылкС всСми лямбдами Π² List<Action>, Π° Π½Π΅ копируСтся Π² ΠΌΠΎΠΌΠ΅Π½Ρ‚ добавлСния.
- Когда Ρ†ΠΈΠΊΠ» Π·Π°Π²Π΅Ρ€ΡˆΠΈΡ‚ΡΡ, i == 5, ΠΈ всС лямбды ΠΎΠ±Ρ€Π°Ρ‰Π°ΡŽΡ‚ΡΡ ΠΊ ΠΎΠ΄Π½ΠΎΠΌΡƒ ΠΈ Ρ‚ΠΎΠΌΡƒ ΠΆΠ΅ `i`, ΡƒΠΆΠ΅ Ρ€Π°Π²Π½ΠΎΠΌΡƒ 5.

---

### βœ… ΠžΠΆΠΈΠ΄Π°Π΅ΠΌΡ‹ΠΉ Π²Ρ‹Π²ΠΎΠ΄:


5 5 5 5 5


πŸ› οΈ Π˜ΡΠΏΡ€Π°Π²Π»Π΅Π½ΠΈΠ΅:

Π§Ρ‚ΠΎΠ±Ρ‹ каТдая лямбда Π·Π°Ρ…Π²Π°Ρ‚Ρ‹Π²Π°Π»Π° свою копию `i`, Π½ΡƒΠΆΠ½ΠΎ Π΄ΠΎΠ±Π°Π²ΠΈΡ‚ΡŒ ΠΏΡ€ΠΎΠΌΠ΅ΠΆΡƒΡ‚ΠΎΡ‡Π½ΡƒΡŽ ΠΏΠ΅Ρ€Π΅ΠΌΠ΅Π½Π½ΡƒΡŽ:


for (int i = 0; i < 5; i++)
{
int copy = i;
actions.Add(() => Console.Write(copy + " "));
}


Π’Π΅ΠΏΠ΅Ρ€ΡŒ Π²Ρ‹Π²ΠΎΠ΄ Π±ΡƒΠ΄Π΅Ρ‚:


0 1 2 3 4


🎯 Π§Ρ‚ΠΎ провСряСт Π·Π°Π΄Π°Ρ‡Π°:

- Π—Π½Π°Π½ΠΈΠ΅ ΠΌΠ΅Ρ…Π°Π½ΠΈΠΊΠΈ Π·Π°ΠΌΡ‹ΠΊΠ°Π½ΠΈΠΉ Π² C#
- ПониманиС Ρ€Π°Π·Π½ΠΈΡ†Ρ‹ ΠΌΠ΅ΠΆΠ΄Ρƒ значСниями ΠΈ ссылками Π² Π·Π°ΠΌΡ‹ΠΊΠ°Π½ΠΈΠΈ
- Π£ΠΌΠ΅Π½ΠΈΠ΅ ΠΎΡ‚Π»Π°ΠΆΠΈΠ²Π°Ρ‚ΡŒ Π½Π΅ΠΎΠΆΠΈΠ΄Π°Π½Π½Ρ‹Π΅ Ρ€Π΅Π·ΡƒΠ»ΡŒΡ‚Π°Ρ‚Ρ‹ Π² LINQ ΠΈ Π΄Π΅Π»Π΅Π³Π°Ρ‚Π°Ρ…

BY C# 1001 notes


Warning: Undefined variable $i in /var/www/tg-me/post.php on line 283

Share with your friend now:
tg-me.com/csharp_1001_notes/687

View MORE
Open in Telegram


C 1001 notes Telegram | DID YOU KNOW?

Date: |

The SSE was the first modern stock exchange to open in China, with trading commencing in 1990. It has now grown to become the largest stock exchange in Asia and the third-largest in the world by market capitalization, which stood at RMB 50.6 trillion (US$7.8 trillion) as of September 2021. Stocks (both A-shares and B-shares), bonds, funds, and derivatives are traded on the exchange. The SEE has two trading boards, the Main Board and the Science and Technology Innovation Board, the latter more commonly known as the STAR Market. The Main Board mainly hosts large, well-established Chinese companies and lists both A-shares and B-shares.

That strategy is the acquisition of a value-priced company by a growth company. Using the growth company's higher-priced stock for the acquisition can produce outsized revenue and earnings growth. Even better is the use of cash, particularly in a growth period when financial aggressiveness is accepted and even positively viewed.he key public rationale behind this strategy is synergy - the 1+1=3 view. In many cases, synergy does occur and is valuable. However, in other cases, particularly as the strategy gains popularity, it doesn't. Joining two different organizations, workforces and cultures is a challenge. Simply putting two separate organizations together necessarily creates disruptions and conflicts that can undermine both operations.

C 1001 notes from ye


Telegram C# 1001 notes
FROM USA